import axios from 'axios';
import {message} from 'antd';
// import {browserHistory ,hashHistory} from 'react-router';

let requestUrl='https://easy-mock.com/mock/5bf7703f0756573d9cb013f5/react-mock-movie';
export const Axios = (method,resourcesName,data,sucFn,errFn)=>{
    console.log('%c('+method+'):%c '+resourcesName+' %c(data):%c '+JSON.stringify(data),"color:blue","","color:blue","");
    let headerData= /token/.test(resourcesName)? {} : {"Auth":"Bearer"+localStorage.getItem('token')};
    console.log(headerData)
    if(/\/undefined/.test(resourcesName)){
        errFn('undefined');
        return
    }
    let requestConfig= (method==='get' || method==='GET')? {
        method:method,
        url:requestUrl+resourcesName,
        params:data,
        headers: headerData
    }:{
        method:method,
        url:requestUrl+resourcesName,
        data:data,
        headers: headerData
    };
    axios(requestConfig).then(function(res){
        console.log('%c('+method+'):%c '+resourcesName+' %c(succ):',"color:green",'',"color:green",res);
        sucFn(res.data);
    }).catch(function (error) {
        console.log('%c('+method+'):%c '+resourcesName+' %c(err):',"color:red;font-weight:700;font-size:14px;",'',"color:red;font-weight:700;font-size:16px;",error);
        if(/status code 403/.test(error)){
            message.error('权限不足');
        }else if(/status code 401/.test(error)){
            message.error('token校验错误');
            // hashHistory.push('/login');
        }else if(/status code 500/.test(error)){
            message.error('系统繁忙');
        }else if(/Network Error/.test(error)){
            message.error('网络错误');
        }
        errFn(error);
    });

}
